package com.yuren.codecrushlearn.Q5400;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Created with Intellij IDEA.
 * Description:
 *  图的邻接表
 * @author 10204
 * @date 2025-06-12 00:27
 */
public class Q5452 {
    private class Edge {
        public int vertex;
        public int weight;

        public Edge(int vertex, int weight) {
            this.vertex = vertex;
            this.weight = weight;
        }
    }

    private int v;
    private List<List<Edge>> matrix;

    public Q5452(int v) {
        this.v = v;
        this.matrix = new ArrayList<>(v);
        for (int i = 0; i < v; i++) {
            this.matrix.add(new ArrayList<>());
        }
    }

    public void addEdge(int v1, int v2, int weight) {
        matrix.get(v1).add(new Edge(v2, weight));
    }

    public int getEdgeWeight(int v1, int v2) {
        for (Edge edge : matrix.get(v1)) {
            if (edge.vertex == v2) {
                return edge.weight;
            }
        }
        return -1;
    }

    public List<Integer> getAdjacentVertices(int v1) {
        List<Integer> res = new ArrayList<>();
        for (Edge edge : matrix.get(v1)) {
            res.add(edge.vertex);
        }
        Collections.sort(res);
        return res;
    }
}
